package org.example.POJO.game;

import lombok.Data;
import org.example.POJO.online.User;

import java.io.Serializable;

@Data
public class Room implements Serializable {
    private String roomId; // 房间唯一ID
    private String user; // 房间创建者
    private String leftAvatar; // 左侧用户的头像路径
    private String rightAvatar; // 右侧用户的头像路径
    private int playcount; // 当前房间玩家人数
    private String[] users = new String[2];

    // 无参构造函数
    public Room() {
        this.roomId = "";
        this.leftAvatar = null;
        this.rightAvatar = null;
        this.playcount = 0; // 初始化人数为 0
    }

    // 全参数构造函数
    public Room(String roomId, String leftUser, String rightUser, String leftAvatar, String rightAvatar) {
        this.roomId = roomId;
        this.leftAvatar = leftAvatar;
        this.rightAvatar = rightAvatar;
    }

    // 部分参数构造函数（只初始化房间ID和默认头像）
    public Room(String roomId) {
        this.roomId = roomId;
        this.leftAvatar = "picture/img/noone.gif"; // 默认左用户头像
        this.rightAvatar = "picture/img/noone.gif"; // 默认右用户头像
        this.playcount = 0; // 初始化人数为 0
    }

    //添加玩家
    public synchronized void addPlayer(String username) {
        if (playcount < 2) {
            users[playcount] = username;
            playcount++;
        }
    }

    //给根据用户名给头像赋值
    public synchronized void setAvatar(String username, String avatar) {
        if (users[0].equals(username)) {
            leftAvatar = avatar;
        } else if (users[1].equals(username)) {
            rightAvatar = avatar;
        }
    }
    //房间清空
    public synchronized void clear() {
        playcount = 0;
        users[0] = "";
        users[1] = "";
        leftAvatar = "picture/img/noone.gif";
        rightAvatar = "picture/img/noone.gif";
    }
}


